package com.sambo.inspection.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonElement;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.support.BaseActivity;
import com.sambo.inspection.bean.GlobleBean;
import com.sambo.inspection.bean.UserInfo;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;

public class UserInfoActivity extends BaseActivity implements View.OnClickListener {

    public static final int CODE_NAME = 0;
    public static final int CODE_TEL = 1;
    public static final int CODE_MAIL = 2;

    ImageView ivBack;
    TextView tvUsername;
    TextView tvInspectorCode;
    RelativeLayout rlRealName;
    TextView tvRealName;
    RelativeLayout rlTel;
    TextView tvTel;
    RelativeLayout rlMail;
    TextView tvMail;

    private int mRequestCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userinfo);

        ivBack = findViewById(R.id.iv_back);
        tvUsername = findViewById(R.id.tv_username);
        tvInspectorCode = findViewById(R.id.tv_inspector_code);
        rlRealName = findViewById(R.id.rl_real_name);
        tvRealName = findViewById(R.id.tv_real_name);
        rlTel = findViewById(R.id.rl_tel);
        tvTel = findViewById(R.id.tv_tel);
        rlMail = findViewById(R.id.rl_mail);
        tvMail = findViewById(R.id.tv_mail);

        tvUsername.setText(GlobleBean.getInstance().getUserName());
        tvInspectorCode.setText(String.valueOf(GlobleBean.getInstance().getUserID()));
        tvRealName.setText(GlobleBean.getInstance().getNickName());
        tvTel.setText(GlobleBean.getInstance().getPhone());
        tvMail.setText(GlobleBean.getInstance().getMail());

        ivBack.setOnClickListener(this);
        rlRealName.setOnClickListener(this);
        rlTel.setOnClickListener(this);
        rlMail.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_real_name:
                String name = tvRealName.getText().toString();
                Intent intent = new Intent(UserInfoActivity.this, ChangeInfoActivity.class);
                intent.putExtra("title", "修改姓名");
                intent.putExtra("content", name);
                intent.putExtra("type", CODE_NAME);
                startActivityForResult(intent, CODE_NAME);
                break;
            case R.id.rl_tel:
                String tel = tvTel.getText().toString();
                Intent intent2 = new Intent(UserInfoActivity.this, ChangeInfoActivity.class);
                intent2.putExtra("title", "修改电话");
                intent2.putExtra("content", tel);
                intent2.putExtra("type", CODE_TEL);
                startActivityForResult(intent2, CODE_TEL);
                break;
            case R.id.rl_mail:
                String mail = tvMail.getText().toString();
                Intent intent3 = new Intent(UserInfoActivity.this, ChangeInfoActivity.class);
                intent3.putExtra("title", "修改邮件");
                intent3.putExtra("content", mail);
                intent3.putExtra("type", CODE_MAIL);
                startActivityForResult(intent3, CODE_MAIL);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            if (data == null) return;
            String content = data.getStringExtra("content");
            mRequestCode = requestCode;

            setUserInfo(content);
        }
    }

    private void setUserInfo(final String content) {

        UserInfo userInfo = new UserInfo();
        userInfo.setId(GlobleBean.getInstance().getUserID());
        userInfo.setUsername(GlobleBean.getInstance().getUserName());
        userInfo.setRealName(GlobleBean.getInstance().getNickName());
        userInfo.setTel(GlobleBean.getInstance().getPhone());
        userInfo.setMail(GlobleBean.getInstance().getMail());

        switch (mRequestCode) {
            case CODE_NAME:
                userInfo.setRealName(content);
                tvRealName.setText(content);
                break;
            case CODE_TEL:
                userInfo.setTel(content);
                tvTel.setText(content);
                break;
            case CODE_MAIL:
                userInfo.setMail(content);
                tvMail.setText(content);
                break;
        }

        HttpClient.post(this, Urls.API_SERVER_HOST + Urls.SET_USER_INFO, userInfo,
                new HttpResponseHandler() {

                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        switch (mRequestCode) {
                            case CODE_NAME:
                                GlobleBean.getInstance().setNickName(content);
                                break;
                            case CODE_TEL:
                                GlobleBean.getInstance().setPhone(content);
                                break;
                            case CODE_MAIL:
                                GlobleBean.getInstance().setMail(content);
                                break;
                        }
                        Toast.makeText(UserInfoActivity.this, "修改用户信息成功", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onInnovationFailure(String msg) {
                        super.onInnovationFailure(msg);
                        Toast.makeText(UserInfoActivity.this, "修改用户信息失败:" + msg, Toast.LENGTH_SHORT).show();
                    }
                }, null);
    }
}
